#include "robot.h"
#include "radar.h"
#include <iostream>
#include <string>

using namespace std;

void Robot::processcommand(string command)
{
	if (command == "move")
	{
		int distance;
		cout << "How far? ";
		cin >> distance;
		if(distance > 0){
			Robot::move(distance);
		}
		else{
			cout << "Distance must be greater than 0" << endl;
		}
	}
	else if (command == "turn")
	{
		string direction;
		cout << "right or left? ";
		cin >> direction;

		if(direction == "right")
		{
			Robot::turnright();
		}
		else if (direction == "left")
		{
			Robot::turnleft();
		}
		else 
		{
			cout << "Invalid direction" << endl;
		}
	}
	else if(command == "quit")
	{
		cout << "quitting" << endl;
		cout << "=================================================" << endl;
	}
	else if(command == "ping"){
		pingRadar();
	}
	else
	{
		cout << "Invalid Command" << endl;
	}
}
string Robot::getname()
{
	return name; 
}

void Robot::printStatus(){

	cout << "---The location of " << getname() << " is ";
	loc.printlocation();
	cout << endl;
	cout << "---Facing ";
	loc.printdirection();
	cout << endl;
}

void Robot::updateWorld(){
	string type = "robot";
	world->updateRobotLocation(type,&loc);
}

void Robot::moveStep(){
	if (loc.getdirection()==Location::north)
	{
		if(!world->isObjectAtLocation(loc.getYlocation()+1,loc.getXlocation())){
			loc.moveY(1);
		}
	}
	else if (loc.getdirection()==Location::east)
	{
		if(!world->isObjectAtLocation(loc.getYlocation(),loc.getXlocation()+1)){
			loc.moveX(1);
		}
	}
	else if (loc.getdirection()==Location::south)
	{
		if(!world->isObjectAtLocation(loc.getYlocation()-1,loc.getXlocation())){
			loc.moveY(-1);
		}
	}
	else if (loc.getdirection()==Location::west)
	{
		if(!world->isObjectAtLocation(loc.getYlocation(),loc.getXlocation()-1)){
			loc.moveX(-1);
		}
	}

	updateWorld();
}

void Robot::pingRadar(){
	int range = 3;
	Radar rad(world,range,loc);

	cout << "----------------------" << endl;
	if(rad.detect()){
		cout << "***Object Detected within " << range << " Units***" << endl;
	}
	else
	{
		cout << "***No Objects Detected within " << range << " Units***" << endl;
	}
}
